<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 字符串常量池深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 4s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.8; }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            margin: 1.5rem 0;
            overflow-x: auto;
            position: relative;
            font-family: 'Consolas', 'Monaco', monospace;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 0.5rem;
            right: 1rem;
            font-size: 0.75rem;
            color: #808080;
            text-transform: uppercase;
            letter-spacing: 0.1em;
        }
        
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 2.5rem;
            height: 2.5rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 700;
            margin-right: 1rem;
            flex-shrink: 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1.5rem;
            border-radius: 12px;
            margin: 2rem 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::after {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
            animation: rotate 10s linear infinite;
        }
        
        @keyframes rotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        
        .mermaid {
            margin: 2rem auto;
            text-align: center;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .feature-icon {
            width: 3rem;
            height: 3rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.5rem;
            margin-bottom: 1rem;
        }
        
        h3 {
            position: relative;
            padding-left: 1rem;
            margin: 2.5rem 0 1.5rem 0;
        }
        
        h3::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6 relative">
        <div class="max-w-6xl mx-auto text-center relative z-10">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                Java 字符串常量池
                <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度解析与最佳实践</span>
            </h1>
            <p class="text-xl md:text-2xl font-light max-w-3xl mx-auto leading-relaxed opacity-95">
                探索 JVM 中最精妙的内存优化机制，理解字符串在 Java 世界中的独特地位
            </p>
            <div class="mt-10 flex justify-center space-x-8">
                <div class="text-center">
                    <i class="fas fa-memory text-4xl mb-2"></i>
                    <p class="text-sm">内存优化</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-bolt text-4xl mb-2"></i>
                    <p class="text-sm">性能提升</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-code text-4xl mb-2"></i>
                    <p class="text-sm">最佳实践</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Java 中，字符串常量池（String Constant Pool）是一个特殊的内存区域，用于存储字符串字面值（string literals）和通过特定方式创建的字符串。这个机制不仅优化了内存使用，还提升了字符串操作的性能。让我们深入探索字符串进入常量池的各种情况。
            </p>
        </div>

        <!-- Visualization -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="text-2xl font-bold mb-6 text-center text-gray-800">字符串常量池机制概览</h2>
            <div class="mermaid">
                graph TD
                    A[字符串创建] --> B{创建方式}
                    B -->|字面值| C[编译时加入常量池]
                    B -->|new关键字| D[存储在堆内存]
                    B -->|常量表达式| E[编译时优化]
                    D -->|intern()| F[手动加入常量池]
                    C --> G[常量池]
                    E --> G
                    F --> G
                    G --> H[内存共享]
                    G --> I[性能优化]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style G fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style H fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style I fill:#f5576c,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Section 1 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <div class="flex items-start mb-6">
                <span class="section-number">1</span>
                <h3 class="text-2xl font-bold text-gray-800 m-0 p-0">编译时字面值创建的字符串</h3>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6 mb-6">
                <div class="bg-purple-50 rounded-xl p-6">
                    <div class="feature-icon">
                        <i class="fas fa-file-code"></i>
                    </div>
                    <h4 class="text-lg font-semibold mb-3 text-gray-800">编译时加入常量池</h4>
                    <p class="text-gray-700">在编译阶段，所有使用字面值形式创建的字符串都会自动放入字符串常量池。这是最常见也是最高效的方式。</p>
                </div>
                <div class="bg-pink-50 rounded-xl p-6">
                    <div class="feature-icon" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
                        <i class="fas fa-share-alt"></i>
                    </div>
                    <h4 class="text-lg font-semibold mb-3 text-gray-800">相同字面值共享</h4>
                    <p class="text-gray-700">JVM 不会在常量池中创建多个相同字符串的副本，而是让所有引用指向同一个对象，实现内存共享。</p>
                </div>
            </div>

            <div class="code-block">
                <pre><code>String str1 = "Hello";
String str2 = "World";
String str3 = "Hello";  // str3 和 str1 指向同一个对象

System.out.println(str1 == str3);  // 输出: true</code></pre>
            </div>
        </div>

        <!-- Section 2 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <div class="flex items-start mb-6">
                <span class="section-number">2</span>
                <h3 class="text-2xl font-bold text-gray-800 m-0 p-0">使用 intern() 方法</h3>
            </div>
            
            <p class="text-lg text-gray-700 mb-6">
                当通过 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">new</code> 关键字创建字符串时，该字符串最初存储在堆内存中。<code class="bg-gray-100 px-2 py-1 rounded text-purple-600">intern()</code> 方法提供了手动将字符串加入常量池的能力。
            </p>

            <div class="highlight-box">
                <h4 class="text-xl font-semibold mb-3 relative z-10">
                    <i class="fas fa-lightbulb mr-2"></i>intern() 方法的工作原理
                </h4>
                <ul class="space-y-2 relative z-10">
                    <li><i class="fas fa-check-circle mr-2"></i>如果常量池中存在相同内容的字符串，返回池中的引用</li>
                    <li><i class="fas fa-check-circle mr-2"></i>如果不存在，将该字符串添加到常量池中，并返回其引用</li>
                </ul>
            </div>

            <div class="code-block">
                <pre><code>String str1 = new String("Hello");  // 堆内存中的对象
String str2 = str1.intern();        // 返回常量池中的引用
String str3 = "Hello";              // 直接引用常量池

System.out.println(str2 == str3);   // 输出: true
System.out.println(str1 == str3);   // 输出: false</code></pre>
            </div>
        </div>

        <!-- Section 3 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <div class="flex items-start mb-6">
                <span class="section-number">3</span>
                <h3 class="text-2xl font-bold text-gray-800 m-0 p-0">字符串常量表达式</h3>
            </div>
            
            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl p-6 mb-6">
                <p class="text-lg text-gray-700">
                    <i class="fas fa-magic text-purple-600 mr-2"></i>
                    编译器会对字符串常量表达式进行优化，将编译时可确定的字符串连接结果直接放入常量池。
                </p>
            </div>

            <div class="grid md:grid-cols-2 gap-6">
                <div>
                    <h4 class="text-lg font-semibold mb-3 text-gray-800">
                        <i class="fas fa-check text-green-500 mr-2"></i>编译时优化
                    </h4>
                    <div class="code-block">
                        <pre><code>// 编译时确定，进入常量